<?php
class p_cast_dictionary extends Model
{
    public static function getTableName()
    {
        return 'p_cast_dictionary';
    }

    public function set($object)/*{{{*/
    {
        $fields = array(
            'id',
            'master', 
            'chi_last_name', 
            'chi_first_name', 
            'eng_last_name', 
            'eng_first_name1', 
            'eng_first_name2', 
            'others', 
            'see_also', 
            'remark', 
            'gender', 
            'is_chinese', 
            'created_at', 
            'created_by', 
            'modified_at', 
            'modified_by'
        );

        foreach ($fields as $field) {
            $this->$field = isset($object->$field) ? $object->$field : '';
        }
    }/*}}}*/

    static function getCastListById(array $id)/*{{{*/
    {
        if (empty($id)) {
            return array();
        }

        $sql = "SELECT `id`, `master` FROM `p_cast_dictionary` WHERE `id` IN (" . implode(',', $id) . ")";
        $ret = Model::selectBySql($sql, array());

        $data = array();
        foreach ($ret as $row) {
            $list = array();
            $list['id'] = $row['id'];
            unset($row['id']);
            foreach ($row as $key=>$value) {
                if (empty($value)) {
                    unset($row[$key]);
                }
            }
            $list['system_label'] = implode(',', $row);
            $data[] = $list;
        }

        return $data;
    }/*}}}*/
    
    public function getResultList($findRelation, $field, $operation, $value, $start, $length, $sidx	= NULL, $sord = NULL, $unpublish = 0)/*{{{*/
    {
        $find =	$findRelation['mapField'];

        #DB connection
        if ($unpublish) {
            Model::$dbo['r'] = CommonUtils::getDbConn(0,'preview');
        }

        $where = array();
        foreach ($field as  $fieldKey=>$fieldValue) {
            $where[] = Model::assembleWhereSentence($find, $fieldValue, $operation[$fieldKey], $value[$fieldKey]);
        }

        $sql = "
            SELECT 
            FROM `p_cast_dictionary`
            WHERE
            ";

        #where
        if (!empty($where)) {
            $sql .= " WHERE " . implode(' AND ', $where);
        }

        #total
		$sql_total = 'SELECT count(*) AS num FROM ('.$sql.') AS tmp';
        $total = Model::selectBySql($sql_total);

		$list =	array();
		$list['page'] =	ceil($start/$length) + 1;
		$list['total'] = ceil($total['0']['num']/$length);
		$list['records'] = $total['0']['num'];

        #order by
        if ($sidx && $sord) {
            $sql .= " ORDER BY {$sidx} {$sord}";
        }

        #limit
        if ($start && $length) {
            $sql .= " LIMIT {$start}, {$length}";
        }

        $result = Model::selectBySql($sql);
		foreach	($result as $row) {
            $button_view = '<a title="View" href=".html?id='. $row['id'] . '&unpublish='. $unpublish . '" class="btn view ui-state-default ui-corner-all"><span class="ui-icon ui-icon-image"/>View</a>';
            $button_edit = $unpublish ? NULL : '<a title="Edit" href=".html?id='. $row['id']. '" class="btn edit ui-state-default ui-corner-all"><span class="ui-icon ui-icon-pencil"/>Edit</a>';

			$tmp = array();
			$tmp['id'] = $row['id']; 
			$tmp['cell'] = array_values($row); 
            $tmp['cell'][] = $button_edit.$button_view;
            
			$list['rows'][] = $tmp;
		}

		return	$list;
	}/*}}}*/

    static public function checkExistsByMasterName($master_name, $id=NULL)/*{{{*/
    {
        if ($master_name && $id) {
            $sql = "SELECT COUNT(*) AS num FROM `p_cast_dictionary` WHERE `master`=:master AND `id`<>:id";
            $result = Model::selectBySql($sql, array(':master' => $master_name, ':id' => $id));
            $ret = $result[0]['num'] ? TRUE : FALSE;
        } else if ($master_name) {
            $sql = "SELECT COUNT(*) AS num FROM `p_cast_dictionary` WHERE `master`=:master";
            $result = Model::selectBySql($sql, array(':master' => $master_name));
            $ret = $result[0]['num'] ? TRUE : FALSE;
        } else {
            $ret = FALSE;
        }

        return $ret;
    }/*}}}*/
}
?>
